package com.hn658.base.message.managermq.send;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.hn658.base.message.appinfo.entity.AppMqConfigEO;
import com.hn658.base.message.appinfo.enums.QueueType;
import com.hn658.base.message.appinfo.service.IAppMqService;
import com.hn658.base.message.managermq.JmsTemplateManager;
import com.hn658.base.message.managermq.MessageDestinationManager;
import com.hn658.base.message.managermq.MessageListenerManager;
import com.hn658.base.message.managermq.container.MqListenerContainer;
import com.hn658.base.message.managermq.contants.MQManagerContants;
import com.hn658.base.message.managermq.destination.IMessageDestinationBean;
import com.hn658.base.message.managermq.destination.impl.MessageDestinationQueue;
import com.hn658.base.message.managermq.destination.impl.MessageDestinationTopic;
import com.hn658.base.message.managermq.init.ActiveMqJmsTemplateInit;
import com.hn658.base.message.managermq.template.MqTemplate;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.utils.JsonMapper;
import com.mongodb.util.Hash;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Destination;

/**
 * 
 * 消息发送器
 * @author ztjie
 * @date 2015-11-20 上午11:19:55
 * @since
 * @version
 */
public class AbstractMessageSend {

	@Autowired
	private MessageDestinationManager messageDestinationManager;

	
	@Autowired
	private ActiveMqJmsTemplateInit activeMqJmsTemplateInit;
    /**
     * Json序列化器
     */
    protected JsonMapper jsonMapper = JsonMapper.nonEmptyMapper();

    @Autowired
    private JmsTemplateManager jmsTemplateManager;
    
    @Autowired
    private IAppMqService appMqService;
    
    private Lock lock = new ReentrantLock();

	@Autowired
	private MessageListenerManager messageListenerManager;
    /**
     * 
     * <p>通过应用ID及MQ队列序号，获得队列处理器</p> 
     * @author ztjie
     * @date 2015-11-19 下午3:08:06
     * @param appId 应用ID
     * @param index 应用配置的MQ队列序号
     * @return
     * @see
     */
    public JmsTemplate getJmsTemplate(String appId, Integer index) {
    	JmsTemplate template = jmsTemplateManager.get(appId, index);
    	if(template==null){
    		Map<String, Object> map=new HashMap<String, Object>();
    		map.put("appId", appId);
    		map.put("queuePriority",index);
    		map.put("isDeleted", false);
    		AppMqConfigEO mq = appMqService.queryUniqueMqConfig(map);
    		if(mq!=null){
    			registerMq(mq);
    			template=getJmsTemplate(appId, index);
    		}
    	}
    	return template;
    }
    /**
	 * spring中添加mq对应的bean
	 */
	public void registerMq(AppMqConfigEO appMq){
		registerDestination(appMq);
		registerTemplate(appMq);
		registerListener(appMq);
	}
	private void registerTemplate(AppMqConfigEO appMq){
		lock.lock();
		try{
			MqTemplate temp = new MqTemplate();
			String key=appMq.getAppId()+"_"+appMq.getQueuePriority();
			temp.setName(MQManagerContants.TEMPLATE+"_"+key);
			temp.setConnectionFactoryName(MQManagerContants.CONNECTIONFACTORY);
			temp.setDefaultDestinationName(MQManagerContants.DESTINATION+"_"+key);
			String pubSubDomainName=appMq.getQueueType()==1?"false":"true";
			temp.setPubSubDomainName(pubSubDomainName);
			jmsTemplateManager.add(appMq.getAppId(), appMq.getQueuePriority(), temp);
		}finally{
			lock.unlock();			
		}
	}
	private void registerDestination(AppMqConfigEO appMq){
		lock.lock();
		try{
			IMessageDestinationBean defaultDestination=null;
			if(appMq.getQueueType().intValue()==QueueType.Queue.getCode().intValue()){
				defaultDestination = new MessageDestinationQueue();
			}else{
				defaultDestination = new MessageDestinationTopic();
			}
			defaultDestination.setDestinationName(appMq.getQueueName());
			messageDestinationManager.add(appMq.getAppId(),appMq.getQueuePriority(), defaultDestination);
		}finally{
			lock.unlock();			
		}
	}
	private void registerListener(AppMqConfigEO appMq){
		lock.lock();
		try{
			MqListenerContainer jmsContainer = new MqListenerContainer();
			String key=appMq.getAppId()+"_"+appMq.getQueuePriority();
			jmsContainer.setConnectionFactoryName(MQManagerContants.CONNECTIONFACTORY);
			jmsContainer.setDestinationName(MQManagerContants.DESTINATION+"_"+key);
			jmsContainer.setMessageListenerName(MQManagerContants.MESSAGELISTENER);
			String pubSubDomainName=appMq.getQueueType()==1?"false":"true";
			jmsContainer.setPubSubDomainName(pubSubDomainName);
			jmsContainer.setPubSubNoLocalName("false");
			jmsContainer.setReceiveTimeoutName("10000");
			MqListenerContainer container = messageListenerManager.add(appMq.getAppId(), appMq.getQueuePriority(), jmsContainer);
			container.start();
		}finally{
			lock.unlock();	
		}
	}
    /**
     * 
     * <p>通过应用ID及MQ队列序号，获得队列处理器</p> 
     * @author ztjie
     * @date 2015-11-19 下午3:08:06
     * @param appId 应用ID
     * @param index 应用配置的MQ队列序号
     * @return
     * @see
     */
    public Destination getDestination(String appId, Integer index) {
    	return messageDestinationManager.get(appId, index);
    }

}
